import matplotlib.pyplot as plt
import numpy as np

# 读取拟合的样条点
def read_fitted_points(file_path):
    x_vals = []
    y_vals = []
    with open(file_path, 'r') as file:
        for line in file:
            x, y = map(float, line.strip().split(','))
            x_vals.append(x)
            y_vals.append(y)
    return x_vals, y_vals

# 定义原始心形函数
def original_heart_shape(n_points=1000):
    t_vals = np.linspace(0, 2 * np.pi, n_points)
    x_vals = 16 * (np.sin(t_vals) ** 3)
    y_vals = 13 * np.cos(t_vals) - 5 * np.cos(2 * t_vals) - 2 * np.cos(3 * t_vals) - np.cos(4 * t_vals)
    return x_vals, y_vals

# 主函数
def main():
    # 原始心形曲线
    x_original, y_original = original_heart_shape()

    # 文件路径和 N 值对应关系（等距节点和累积弦长）
    equidistant_files = {
        10: "E1_fitted_spline_equidistant_N10.txt",
        40: "E1_fitted_spline_equidistant_N40.txt",
        160: "E1_fitted_spline_equidistant_N160.txt"
    }


    plt.figure(figsize=(12, 8))

    # 绘制原始心形曲线
    plt.plot(x_original, y_original, 'k--', label="Original Heart Shape", alpha=0.8)

    # 绘制不同 N 值下的拟合曲线（等距节点）
    for n, file_path in equidistant_files.items():
        x_spline, y_spline = read_fitted_points(file_path)
        plt.plot(x_spline, y_spline, label=f"Equidistant Spline (N={n})")

    # 添加图例和标题
    plt.legend()
    plt.title("Spline Fitting with Equidistant and Chordal Nodes")
    plt.xlabel("X")
    plt.ylabel("Y")
    plt.grid(True)
    plt.axis("equal")
    plt.savefig(f'../figure/E1_EN.png')
    # 显示图像
    plt.show()

if __name__ == "__main__":
    main()
